package HeSuanServer.service.Impl;

import HeSuanServer.POJO.Employee;
import HeSuanServer.POJO.R.Result;
import HeSuanServer.POJO.R.StatusCode;
import HeSuanServer.POJO.User;
import HeSuanServer.POJO.UserMessage;
import HeSuanServer.mapper.EmpCliMapper;
import HeSuanServer.service.EmpCliService;
import HeSuanServer.service.UmService;
import cn.hutool.core.date.DateTime;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @描述:
 * @author: LH
 * @date: 2023/2/16
 */
@Slf4j
@Service
public class EmpCliServiceImpl implements EmpCliService {
    @Resource
    private EmpCliMapper empCliMapper;
    @Resource
    private UmService umservice;
    @Override
    public Result code(String msg) {
        Result result=new Result();
        String[] split = msg.split("!@!");
        log.info(split.toString());
        result.setData(split);
        return result;
    }
    @Override
    public Result login(Employee emp) {
        Result result=new Result();
        Employee employee=empCliMapper.login(emp);
        if (employee==null){
            result.setMsg("用户名或密码错误");
            result.setStatusCode(StatusCode.SQL_EXP_SELECT);
            return result;
        }
        return Result.Builder().Data(employee).StatusCode(StatusCode.SUCCESS).Msg("登录成功");
    }
    @Override
    public Result updatemm(String password, Integer id) {
        try {
            Employee employee=new Employee();
            employee.setPassword(password);
            employee.setId(id);
            empCliMapper.updatemm(employee);
        }catch (Exception e) {
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_UPDATE);
        }
        return Result.Builder().Msg("修改成功").StatusCode(StatusCode.SUCCESS);
    }
    @Override
    public Result getPickById(String pid, String ms) {
        UserMessage userMessage=new UserMessage();
        userMessage.setPickId(pid);
        userMessage.setPickStatus(ms);
        try{
            Integer count=empCliMapper.getPickById(userMessage);
            if ("1".equals(ms)&&count==1){
                return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT_COUNT);
            }
            if ("2".equals(ms)&&count>=10){
                return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT_COUNT);
            }
            return Result.Builder().Data(count).StatusCode(StatusCode.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT);
        }
    }
    @Transactional
    @Override
    public Result luru(Integer uid, String ms, Integer empId,String pipId) {
        User user=empCliMapper.getUser(uid);
        UserMessage us=new UserMessage();
        UserMessage user1 = us.setUser(us, user);
        user1.setPickId(pipId);
        user1.setEmpId(empId);
        user1.setDate(new Date());
        user1.setPickStatus(ms);
        user1.setResult("0");
        umservice.add(user1);
        return Result.Builder().StatusCode(StatusCode.SUCCESS).Msg("信息录入成功");
    }

    @Override
    public Result userById(Integer id) {
        User user = empCliMapper.getUser(id);
        return Result.Builder().StatusCode(StatusCode.SUCCESS).Data(user);
    }
    /**
    * @描述:
    * @参数: [empId]
    * @返回值: HeSuanServer.POJO.R.Result
    * @作者: LH
    * @日期: 2023/3/9
    */
    @Override
    public Result jilu(Integer empId) {
        try {
            DateTime startDateTime=new DateTime();
            startDateTime.setHours(0);
            startDateTime.setMinutes(0);
            startDateTime.setSeconds(0);
            DateTime endDateTime=new DateTime();
            endDateTime.setHours(23);
            endDateTime.setMinutes(59);
            endDateTime.setSeconds(59);
            Map<String,Object> map=new HashMap();
            map.put("empId",empId);
            map.put("start",startDateTime);
            map.put("end",endDateTime);
            List<Map<String,Object>> list=empCliMapper.jilu(map);
            for (int i = 0; i < list.size(); i++) {
                String pickStatus = Objects.equals((String) list.get(i).get("pickStatus"), "1") ?"单人":"混合";
                list.get(i).put("pickStatus",pickStatus);
                Map<String, Object> map1 = list.get(i);
                LocalDateTime date = (LocalDateTime) map1.get("date");
//                SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                map1.put("date",date.format(DateTimeFormatter.ofPattern("yyy-MM-dd HH:mm")));
            }
            return Result.Builder().Data(list).StatusCode(StatusCode.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT);
        }
    }

    @Override
    public Result sd(User user,String ms, Integer empId,String pipId) {
//        判断数据库中是否存在该用户
        User byUid = empCliMapper.getUserByUid(user.getUid());
        Integer id;
        if (byUid==null){
            log.warn("该用户不存在");
            log.info("开始创建用户");
            empCliMapper.add(user);
            id=user.getId();
            log.info("创建用户成功");
            return luru(id, ms, empId, pipId);
        }else {
            id=byUid.getId();
//        写入数据
            return luru(id, ms, empId, pipId);
        }
    }

//    public static void main(String[] args) {
//        DateTime endDateTime=new DateTime();
//        endDateTime.setHours(23);
//        endDateTime.setMinutes(59);
//        endDateTime.setSeconds(59);
//        System.out.println(endDateTime);
//    }
}
